<?php 
if ( ! defined( 'ABSPATH' ) ) {
	die();
}
 // wp_enqueue_style( 'ifan-responsive', get_template_directory_uri().'/inc/vendor-config/redux/repair.css' );
// 判断是否VIP
if (1==1) {
    require_once get_template_directory(). '/inc/core/theme-install.php';
}else{
    require_once get_template_directory(). '/inc/core/theme-install.php';
}
    require_once get_template_directory(). '/inc/class/class-ws-post-type.php';
    require_once get_template_directory(). '/inc/class/class-custom-seo-filed.php';
    require_once get_template_directory(). '/inc/core/categories-images.php';
// require_once get_template_directory(). '/inc/vendor-config/taxonomy-type.php';
// category

// 注销掉默认的文章类型
// function wpdaxue_deregister_post_type(){
//   unregister_post_type( 'post' ); //注销faq文章类型
//   unregister_taxonomy( 'category'); //注销 faq_category 分类法
// }
// add_action('init','wpdaxue_deregister_post_type');


$newtype=array(
    // [
    //     'taxonomie'=>'case',
    //     'taxonomietype'=>'cases',
    //     'title'         =>'案例',
    //     'menu_icon' =>'dashicons-editor-kitchensink'
    // ],
    // [
    //     'taxonomie'=>'product',
    //     'taxonomietype'=>'products',
    //     'title'         =>'产品',
    //     'menu_icon' =>'dashicons-cart'
    // ],
    // // [
    // //     'taxonomie'=>'Post',
    // //     'taxonomietype'=>'category',
    // //     'title'         =>'文章',
    // //     'menu_icon' =>''
    // // ],
    // [
    //     'taxonomie'=>'news',
    //     'taxonomietype'=>'newss',
    //     'title'         =>'新闻',
    //     'menu_icon' =>''
    // ],
);

// foreach ($newtype as $key => $value) {

//     new Ws_Post_Type($value['taxonomie'],$value['taxonomietype'],$value['title'],$value['menu_icon']);
//     add_action($value["taxonomietype"].'_add_form_fields', 'ifan_add_category_field', 10, 2);
//     add_action($value["taxonomietype"].'_edit_form_fields', 'ifan_edit_category_field', 10, 2);
//     add_action('created_'.$value["taxonomietype"], 'ifan_category_save', 10, 1);
//     add_action('edited_'.$value["taxonomietype"], 'ifan_category_save', 10, 1);
// }

// new Ws_Post_Type('case','cases','案例');
// new Ws_Post_Type('product','products','产品');


function register_navwalker(){
    require_once get_template_directory() . '/inc/class/class-wp-bootstrap-navwalker.php';
}
add_action( 'after_setup_theme', 'register_navwalker' );


// 定义导航 By laobuluo.com
function itbulu_register_menus() {
    register_nav_menus( array(
        'header-menu' => '网站导航',
        'footer-menu' => '页脚导航左',
        'footer-menu2' => '页脚导航右',
    ) );
}
// 将自定义函数挂载到after_setup_theme钩子下
add_action('after_setup_theme', 'itbulu_register_menus');













if ( ! function_exists( 'add_script_style' ) ) {
    function add_script_style() {
        /* Register & Enqueue Styles. */
        wp_register_style( 'my-style', get_template_directory_uri().'/inc/vendor-config/redux/repair.css' );
        wp_enqueue_style( 'my-style' );

    }
}
add_action( 'admin_print_scripts', 'add_script_style', 10 );

//v2ex国内gravatar头像缓存
function get_ssl_avatar($avatar){ 
    $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://cdn.v2ex.com/gravatar/$1?s=$2" class="avatar avatar-$2" height="50px" width="50px">',$avatar);
    return $avatar; 
} 
add_filter('get_avatar', 'get_ssl_avatar');

function tutsplus_admin_css() {
    // if ( $_GET['action'] === 'register' ) {
    //     wp_enqueue_style( 'register_css', get_template_directory_uri() . '/admin/css/register-form.css' );
    // } else {
        wp_enqueue_style( 'login_css', get_template_directory_uri() . '/admin/css/custom-login.css' );
    // }
}
add_action('login_head', 'tutsplus_admin_css');


function ifan_taxonomy_list($taxonomyname='case',$pagenum=12,$template_part="")
{
    

$post_type = $post->post_type;
$postid=get_queried_object_id();
$postname=get_term( get_queried_object_id(), $taxonomy );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => $post_type, //自定义文章类型名称

        'posts_per_page'=>$pagenum,
        'paged'=>$paged,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomyname,//自定义分类法名称
                'terms' => $postid //id为64的分类。也可是多个分类array(12,64)
                ),
            )
        );
    $my_query = new WP_Query($args);

if($my_query->have_posts()):while($my_query->have_posts()):$my_query->the_post();

 get_template_part($template_part, get_post_type() );


endwhile;
endif;

 wp_reset_postdata(); 
}
add_theme_support( 'post-thumbnails' ); 

// 分页
function mo_paging() {
    $p = 3;
    if ( is_singular() ) return;
    global $wp_query, $paged;
    $max_page = $wp_query->max_num_pages;
    if ( $max_page == 1 ) return; 
    echo '';
    if ( empty( $paged ) ) $paged = 1;
    $link_icon_pre='
    <svg id="icon_left_arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
      <rect id="icon_left_arrow_background" width="24" height="24" fill="none"></rect>
      <path d="M0,10A10.011,10.011,0,0,0-10,0,10.011,10.011,0,0,0-20,10,10.011,10.011,0,0,0-10,20,10.011,10.011,0,0,0,0,10Zm-1.6,0A8.409,8.409,0,0,1-10,18.4,8.409,8.409,0,0,1-18.4,10,8.409,8.409,0,0,1-10,1.6,8.409,8.409,0,0,1-1.6,10Zm-8.834,5.341a.8.8,0,0,0,.077-1.042l-.077-.089L-14.043,10.6H-4V9H-14.07l3.635-3.634a.8.8,0,0,0,.077-1.042l-.077-.089a.8.8,0,0,0-1.041-.077l-.09.077-4.987,4.987a.8.8,0,0,0-.077,1.042l.077.089,4.987,4.988a.8.8,0,0,0,.566.234A.8.8,0,0,0-10.435,15.341Z" transform="translate(22 2)" fill="rgba(66,66,66,0.8)"></path>
      <rect width="24" height="24" fill="rgba(216,216,216,0)"></rect>
    </svg>
          ';

    previous_posts_link('上一页'); 

    if ( $paged > $p + 1 ) _paging_link( 1, '<li>第一页</li>' );
    if ( $paged > $p + 2 ) echo "<span>···</span>";
    for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { 
        if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span>{$i}</span>" : _paging_link( $i );
    }
    if ( $paged < $max_page - $p - 1 ) echo "<span> ... </span>";
    $link_icon_next='
    <svg id="icon_right_arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
      <rect id="icon_right_arrow_background" width="24" height="24" fill="none"></rect>
      <path d="M0,10A10,10,0,1,1,10,20,10.011,10.011,0,0,1,0,10Zm1.6,0A8.4,8.4,0,1,0,10,1.6,8.409,8.409,0,0,0,1.6,10Zm8.834,5.341a.8.8,0,0,1-.077-1.042l.077-.089L14.043,10.6H4V9H14.07L10.435,5.366a.8.8,0,0,1-.077-1.042l.077-.089a.8.8,0,0,1,1.041-.077l.09.077,4.987,4.987a.8.8,0,0,1,.077,1.042l-.077.089-4.987,4.988a.8.8,0,0,1-1.131,0Z" transform="translate(2 2)" fill="rgba(66,66,66,0.8)"></path>
      <rect width="24" height="24" fill="rgba(66,66,66,0)"></rect>
    </svg>
    
    
          ';

    next_posts_link('下一页'); 

    // echo '<span>共 '.$max_page.' 页</span>';

}
function mo_paging2() {
    $p = 3;
    if ( is_singular() ) return;
    global $wp_query, $paged;
    $max_page = $wp_query->max_num_pages;
    if ( $max_page == 1 ) return; 
    echo '';
    if ( empty( $paged ) ) $paged = 1;
    $link_icon_pre='
    <svg id="icon_left_arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
      <rect id="icon_left_arrow_background" width="24" height="24" fill="none"></rect>
      <path d="M0,10A10.011,10.011,0,0,0-10,0,10.011,10.011,0,0,0-20,10,10.011,10.011,0,0,0-10,20,10.011,10.011,0,0,0,0,10Zm-1.6,0A8.409,8.409,0,0,1-10,18.4,8.409,8.409,0,0,1-18.4,10,8.409,8.409,0,0,1-10,1.6,8.409,8.409,0,0,1-1.6,10Zm-8.834,5.341a.8.8,0,0,0,.077-1.042l-.077-.089L-14.043,10.6H-4V9H-14.07l3.635-3.634a.8.8,0,0,0,.077-1.042l-.077-.089a.8.8,0,0,0-1.041-.077l-.09.077-4.987,4.987a.8.8,0,0,0-.077,1.042l.077.089,4.987,4.988a.8.8,0,0,0,.566.234A.8.8,0,0,0-10.435,15.341Z" transform="translate(22 2)" fill="rgba(66,66,66,0.8)"></path>
      <rect width="24" height="24" fill="rgba(216,216,216,0)"></rect>
    </svg>
          ';

    previous_posts_link($link_icon_pre); 

    if ( $paged > $p + 1 ) _paging_link( 1, '<li>第一页</li>' );
    if ( $paged > $p + 2 ) echo "<span>···</span>";
    for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { 
        if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span>{$i}</span>" : _paging_link( $i );
    }
    if ( $paged < $max_page - $p - 1 ) echo "<span> ... </span>";
    $link_icon_next='
    <svg id="icon_right_arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
      <rect id="icon_right_arrow_background" width="24" height="24" fill="none"></rect>
      <path d="M0,10A10,10,0,1,1,10,20,10.011,10.011,0,0,1,0,10Zm1.6,0A8.4,8.4,0,1,0,10,1.6,8.409,8.409,0,0,0,1.6,10Zm8.834,5.341a.8.8,0,0,1-.077-1.042l.077-.089L14.043,10.6H4V9H14.07L10.435,5.366a.8.8,0,0,1-.077-1.042l.077-.089a.8.8,0,0,1,1.041-.077l.09.077,4.987,4.987a.8.8,0,0,1,.077,1.042l-.077.089-4.987,4.988a.8.8,0,0,1-1.131,0Z" transform="translate(2 2)" fill="rgba(66,66,66,0.8)"></path>
      <rect width="24" height="24" fill="rgba(66,66,66,0)"></rect>
    </svg>
    
    
          ';

    next_posts_link($link_icon_next); 

    // echo '<span>共 '.$max_page.' 页</span>';

}
function _paging_link( $i, $title = '' ) {
    if ( $title == '' ) $title = "第 {$i} 页";
    echo "<a href='", esc_html( get_pagenum_link( $i ) ), "'><span class='pagenum'>{$i}</span></a>";
}




// require_once get_template_directory(). '/inc/core/categories-edit-form.php';

//纯代码去除category

/*显示文章浏览次数*/
function getPostViews($postID){
    $count = get_post_meta($postID,'views', true);
    if($count==''){
        delete_post_meta($postID,'views');
        add_post_meta($postID,'views', '0');
        return "0";
    }
    return $count.'';
    }
function setPostViews($postID) {
    $count = get_post_meta($postID,'views', true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID,'views');
        add_post_meta($postID,'views', '0');
    }else{
        $count++;
        update_post_meta($postID,'views', $count);
    }
}

//    if( function_exists('register_sidebar') ) {
//            register_sidebar(array(
//                'name' =>'侧边栏',
//                'id'     =>'sidebar-1',
//                'before_widget' => '<div class="widget">',
//                'after_widget' => '</div>',
//                'before_title' => '<h6>',
//                'after_title' => '</h6>'
//            ));
//        }


/**
 * wordpress分类目录添加可视化编辑器
 * 
 */
// 移除HTML过滤


// add_action('category_add_form_fields', 'ifan_add_category_field', 10, 2);
// add_action('cases_add_form_fields', 'ifan_add_category_field', 10, 2);
// add_action('products_add_form_fields', 'ifan_add_category_field', 10, 2);

// add_action('category_edit_form_fields', 'ifan_edit_category_field', 10, 2);
// add_action('cases_edit_form_fields', 'ifan_edit_category_field', 10, 2);
// add_action('products_edit_form_fields', 'ifan_edit_category_field', 10, 2);

// add_action('created_category', 'ifan_category_save', 10, 1);
// add_action('created_cases', 'ifan_category_save', 10, 1);
// add_action('created_products', 'ifan_category_save', 10, 1);

// add_action('edited_category', 'ifan_category_save', 10, 1);
// add_action('edited_cases', 'ifan_category_save', 10, 1);
// add_action('edited_products', 'ifan_category_save', 10, 1);


function assoc_substr($sourcestr, $cutlength) {
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen ( $sourcestr ); //字符串的字节数 
    while ( ($n < $cutlength) and ($i <= $str_length) ) {
     $temp_str = substr ( $sourcestr, $i, 1 );
     $ascnum = Ord ( $temp_str ); //得到字符串中第$i位字符的ascii码 
     if ($ascnum >= 224) {//如果ASCII位高与224，
        $returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); //根据UTF-8编码规范，将3个连续的字符计为单个字符  
        $i = $i + 3; //实际Byte计为3
        $n ++; //字串长度计1
     } elseif ($ascnum >= 192){ //如果ASCII位高与192，
        $returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); //根据UTF-8编码规范，将2个连续的字符计为单个字符 
        $i = $i + 2; //实际Byte计为2
        $n ++; //字串长度计1
     } elseif ($ascnum >= 65 && $ascnum <= 90) {//如果是大写字母，
      $returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
      $i = $i + 1; //实际的Byte数仍计1个
      $n ++; //但考虑整体美观，大写字母计成一个高位字符
     }elseif ($ascnum >= 97 && $ascnum <= 122) {
       $returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
       $i = $i + 1; //实际的Byte数仍计1个
       $n ++; //但考虑整体美观，大写字母计成一个高位字符
     } else {//其他情况下，半角标点符号，
      $returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
      $i = $i + 1; 
      $n = $n + 0.5; 
     }
    }
    return $returnstr;
 }

 add_theme_support( 'block-templates' );

//  * 新增WordPress小工具区域
//  */
function liangshare_widgets_init() {
    register_sidebar( array(
        'name' => __( '首页5图广告位', 'ifan' ),
        'id' => 'index_adds',
        'description' => __( '首页广告位', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' => __( '首页百科广告位', 'ifan' ),
        'id' => 'index_adds_baike',
        'description' => __( '首页百科广告位', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' => __( '侧边栏产品轮播', 'ifan' ),
        'id' => 'sidebar-product',
        'description' => __( '侧边栏产品轮播', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' =>__( '侧边栏商城按钮', 'ifan'),
        'id' => 'sidebar-article',
        'description' => __( '侧边栏商城按钮', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' =>__( '普通文章中部商品', 'ifan'),
        'id' => 'middle-article',
        'description' => __( '普通文章中部商品', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' =>__( '页脚友情链接', 'ifan'),
        'id' => 'sidebar-friendlink',
        'description' => __( '页脚友情链接', 'ifan' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    }
add_action( 'widgets_init', 'liangshare_widgets_init' );
function get_favicon()
{
    return $ifan_option['favicon_basic'];
}

function tx_wp_get_category_root_id($cat)
{
    $this_category = get_category($cat);//获取当前分类的对象
    //循环往上获得获得父级分类id
    while ($this_category->category_parent) {
        $this_category = get_category($this_category->category_parent);
    }
    return $this_category->term_id;
}


	








//分页

$indexlistid = 'page_id='.get_option('wpd_indexlist');
$searchlistid = 'page_id='.get_option('wpd_searchlist');
function lingfeng_custom_pagenavi( $custom_query,$range = 4 ) {
    global $paged,$wp_query;
    if ( !$max_page ) {
        $max_page = $custom_query->max_num_pages;
    }
    if( $max_page >1 ) {
        echo '<div class="pagination">';
        if( !$paged ){
            $paged = 1;
        }
        if( $paged != 1 ) {
            echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link(1))."' class='extend' title='跳转到首页'>最前</a>";
        }

        if ( $max_page >$range ) {
            if( $paged <$range ) {
                for( $i = 1; $i <= ($range +1); $i++ ) {
                echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link($i))) ."'";
                if($i==$paged) echo " class='active'";echo ">$i</a>";
                }
            }elseif($paged >= ($max_page -ceil(($range/2)))){
            for($i = $max_page -$range;$i <= $max_page;$i++){
                    echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link($i)) ."'";
                if($i==$paged)echo " class='active'";echo ">$i</a>";
                }
            }elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
                for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
                    echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link($i)) ."'";if($i==$paged) echo " class='active'";echo ">$i</a>";
                }
            }
        }else{
        for($i = 1;$i <= $max_page;$i++){
        echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link($i)) ."'";
        if($i==$paged)echo " class='active'";echo ">$i</a>";
        }
        }

        if($paged != $max_page){
        echo "<a href='".str_replace(array($indexlistid,$searchlistid),'',get_pagenum_link($max_page))."' class='extend' title='跳转到最后一页'>最后</a>";
        }
        echo '<span>共['.$max_page.']页</span>';
        echo "</div>";
    }
}

function shopping_func($atts)
{
    extract(shortcode_atts(array(

        'price'=>'998',
        'title'=>'测试用的标题',
        'href'=>'http://www.baidu.com',
        'target'=>'淘宝',
        'img_path'=>'https://www.itouying.cn/wp-content/uploads/2022/06/%E7%99%BE%E7%A7%91%E5%B9%BF%E5%91%8A.jpg'
	), $atts));
   $html='<div class="RichText-MCNLinkCardContainer"><a class="GoodsRecommendCard"  href="'.$href.'" target="_blank" rel="noopener noreferrer noffllow">
        <div class="CPSCommonCard">
            <div class="CPSCommonCard-imageBox CPSCommonCard-imageBox--square">
                <img class="CPSCommonCard-image" src="'.$img_path.'" alt="">
            </div>
            <div class="CPSCommonCard-info">
                <div class="CPSCommonCard-title shenglue">'.$title.'</div>
                <div class="CPSCommonCard-subtitle">
                    <div class="CPSCommonCard-subtitle-text">'.$target.'</div>
                </div>
                <div class="CPSCommonCard-tool">
                    <div class="CPSCommonCard-toolLeft">
                        <div class="GoodsRecommendCardPrice"><span class="GoodsRecommendCardPrice-sell">¥'.$price.'.00</span>
                        </div>
                    </div>
                    <div class="CPSCommonCard-button">去购买<span
                            style="display: inline-flex; align-items: center;">&ZeroWidthSpace;<svg width="24"
                                height="24" viewBox="0 0 24 24" data-new-api="ArrowRightSmall24"
                                data-old-api="ArrowRight" class="Zi Zi--ArrowRight css-339j4i" fill="currentColor">
                                <path
                                    d="M13.248 12l-4.025 3.78a.684.684 0 000 1.01.796.796 0 001.075 0l4.42-4.15a.867.867 0 000-1.28l-4.42-4.15a.796.796 0 00-1.075 0 .684.684 0 000 1.01L13.248 12z"
                                    fill-rule="evenodd" clip-rule="evenodd"></path>
                            </svg></span></div>
                </div>
            </div>
        </div>
    </a>
</div>';
echo $html;

}

add_action( 'init', 'register_shortcodes_shopping',10);

function register_shortcodes_shopping()
{
    add_shortcode('shopping', 'shopping_func');
}


function ifan_button() {
    //判断用户是否有编辑文章和页面的权限
    if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    return;
    }  //判断用户是否使用可视化编辑器
    if ( get_user_option('rich_editing') == 'true' ) {
    
    add_filter( 'mce_external_plugins', 'add_plugin' );
    add_filter( 'mce_buttons', 'register_button' );
    }
    }
    add_action('init', 'ifan_button');
    
    function register_button( $buttons ) {
    array_push( $buttons, "|", "ifan1" ); //添加 一个ifan1 按钮

    
    return $buttons;
    }
    function add_plugin( $plugin_array ) {
    
    //ifan2按钮的js路径
    $plugin_array['ifan1'] = get_bloginfo( 'template_url' ) . '/assets/js/ifan1.js';
    

    return $plugin_array;
    }

